package com.jamin.aabill.ui;


import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.jamin.aabill.R;
import com.jamin.aabill.dao.CircleDao;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.manager.CircleManager;
import com.jamin.aabill.utils.AppConstant;
import com.jamin.aabill.utils.Utils;

/**
 * 
 * ClassName:SubAABillActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-7-24	下午4:42:53
 */
public class SubAACircleActivity extends Activity implements OnItemClickListener , OnCreateContextMenuListener {

	private static final String TAG = "SubAABillActivity=====wangjieming";
	private static final int MENU_DELETE_CIRCLE = 0;
	private static final int MENU_MODIFY_CIRCLE = 1;
	//	private static final int MENU_CLOSE_CIRCLE = 2;

	private static final int DIALOG_DELETE_CIRCLE = 0;


	BroadcastReceiver receiver = null;
	private long circleId = -1;

	//ListView
	ListView mCircleListView = null;
	CircleAdapter mCircleAdapter = null;
	CircleDao mCircleDao = null;
	List<Circle> circleList = null;
	//TextView
	TextView mNoCircleTextView = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_aa_circle_activity);
		initView();
		initData();
		refreshView();
		receiver = new RefreshBroadcastReceiver();
		registerReceiver(receiver, new IntentFilter(AppConstant.BILL_CIRCLE_REFRESH_ACTION));
	}

	private void initData() {
		mCircleDao = new CircleDao();
		circleList = mCircleDao.getCircleList();
		//CircleListView
		mCircleAdapter = new CircleAdapter(circleList, this);
		mCircleListView.setAdapter(mCircleAdapter);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	private void initView() {
		mNoCircleTextView = (TextView) findViewById(R.id.aa_circle_no_list_tv);
		mCircleListView = (ListView) findViewById(R.id.aa_circle_list);
		mCircleListView.setOnItemClickListener(this);
		mCircleListView.setOnCreateContextMenuListener(this);
	}


	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
		Intent intent = new Intent(SubAACircleActivity.this , CircleDetailActivityGroup.class);
		long id =((Circle)mCircleAdapter.getItem(position)).getId();
		Utils.log(TAG, "circleId = " + id);
		intent.putExtra("circleId", id );
		startActivity(intent);
	}




	//列表长按菜单
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		circleId = Long.parseLong(((TextView) menuInfo.targetView.findViewById(R.id.circle_item_id)).getText().toString());
		Utils.log(TAG, "circleId = " + circleId);
		Utils.log(TAG, "item.getItemId() = " + item.getItemId());
		switch(item.getItemId()){
		case MENU_DELETE_CIRCLE:
			showDialog(DIALOG_DELETE_CIRCLE);
			break;
		case MENU_MODIFY_CIRCLE:
			Intent intent = new Intent(SubAACircleActivity.this , CreateCircleActivity.class);
			intent.putExtra("circleId", circleId);
			startActivity(intent);
			break;
			//		case MENU_CLOSE_CIRCLE:
			//			CircleDao mCircleDao = new CircleDao();
			//			
			//			break;
		}
		return super.onContextItemSelected(item);
	}



	@Override
	public void onCreateContextMenu(ContextMenu menu , View view ,ContextMenuInfo menuInfo ) {
		super.onCreateContextMenu(menu , view , menuInfo);
		menu.setHeaderTitle(getString(R.string.aa_bill_menu_cirlce_title));     
		menu.add(0, MENU_DELETE_CIRCLE , 0 , getString(R.string.aa_bill_menu_delete_circle) );  
		menu.add(0, MENU_MODIFY_CIRCLE , 0 , getString(R.string.aa_bill_menu_modify_circle) );  
		//		menu.add(0, MENU_CLOSE_CIRCLE , 0 , getString(R.string.aa_bill_menu_close_circle) );  
	}



	@Override
	protected Dialog onCreateDialog(int id) {

		switch(id){
		case DIALOG_DELETE_CIRCLE:
			Dialog dialog = new AlertDialog.Builder(this)
			.setTitle(getString(R.string.aa_bill_dialog_delete_circle_title))
			.setMessage(getString(R.string.aa_bill_dialog_delete_circle_msg))
			.setPositiveButton(getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					CircleManager manager = new CircleManager();
					manager.deleteCircle(circleId);
					//刷新账单列表
					refreshView();
				}
			})
			.setNegativeButton(getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			}).create();
			dialog.show();
			return dialog;

		}

		return super.onCreateDialog(id);
	}




	/**
	 * 刷新页面广播<br>
	 * ClassName:RefreshBroadcastReceiver
	 *
	 * @author   wangjieming
	 * @version  1.0
	 * @since    v1.0
	 * @Date	 2012-9-19	下午1:55:36
	 */
	class RefreshBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context ctx, Intent intent) {
			if(intent.getAction().equals(AppConstant.BILL_CIRCLE_REFRESH_ACTION)){
				refreshView();
			}
		}

	}


	/**
	 * 页面重新载入，重新装填CircleList<br>
	 * @method updateView
	 * @throws 
	 * @since v1.0
	 */
	private void refreshView() {
		circleList = mCircleDao.getCircleList();
		if(circleList.size() == 0){
			mNoCircleTextView.setVisibility(View.VISIBLE);
		}else{
			mNoCircleTextView.setVisibility(View.GONE);
		}
		mCircleAdapter.notifyAdapter(circleList);
		mCircleAdapter.notifyDataSetChanged();
	}

}

